#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
主题: 在字符串中处理html和xml
问题: 你想将HTML或者XML实体如 &entity; 或 &#code; 替换为对应的文本。 再者，你需要转换文本中特定的字符(比如<, >, 或 &)。
提示:
"""

import html

def recipe1():
    s = 'Elements are written as "<tag>text</tag>".'
    print(f"{s = }")
    print(f"{html.escape(s) = }")

    # Disable escaping of quotes
    print(f"{html.escape(s, quote=False) = }")

def recipe2():
    """将非ASCII文本对应的编码实体嵌入"""
    s = 'Spicy Jalapeño'
    print(f"{s = }")
    print(f"{s.encode('ascii', errors='xmlcharrefreplace') = }")

def recipe3():
    s = 'Spicy &quot;Jalape&#241;o&quot.'
    print(f"{s = }")
    print(f"{html.unescape(s) = }")

    t = 'The prompt is &gt;&gt;&gt;'
    print(f"{t = }")
    from xml.sax.saxutils import unescape
    print(f"{unescape(t) = }")

def main():
    print('recipe1'.center(20, '*'))
    recipe1()
    print('recipe2'.center(20, '*'))
    recipe2()
    print('recipe3'.center(20, '*'))
    recipe3()

if __name__ == '__main__':
    main()      